package com.jiyi.activity.datainput.jm;

import android.app.Dialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.jiyi.bean.datainput.DataInputBaseBean;
import com.jiyi.common.StringInterface;
import com.jiyi.common.VolleyRequest;
import com.jiyi.easemob.util.UserHelp;
import com.jiyi.inteface.WebApi;
import com.jiyi.view.DateChooseDialog;
import com.swift.base.ActivityBase;
import com.swift.base.ApplicationBase;
import com.swift.maintenance.R;
import com.swift.maintenance.base.AppConfig;
import com.swift.maintenance.utils.ToastUtil;
import com.swift.maintenance.utils.UserInfoUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class DataInputBaseReadActivity extends ActivityBase {

    /* 头部 */
    @BindView(R.id.tvBack)
    TextView tvBack;
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvSearch)
    TextView tvSearch;

    @BindView(R.id.tvName)
    TextView tvName;
    @BindView(R.id.tvAddress)
    TextView tvAddress;
    @BindView(R.id.tvBuildType)
    TextView tvBuildType;
    @BindView(R.id.tvCreate)
    TextView tvCreate;
    @BindView(R.id.tvHeight)
    TextView tvHeight;
    @BindView(R.id.tvMianJi)
    TextView tvMianJi;
    @BindView(R.id.tvWYGLQK)
    TextView tvWYGLQK;
    @BindView(R.id.tvJLR)
    TextView tvJLR;
    @BindView(R.id.tvJLRTel)
    TextView tvJLRTel;
    @BindView(R.id.tvJSQK)
    TextView tvJSQK;
    @BindView(R.id.tvCZQK)
    TextView tvCZQK;
    @BindView(R.id.tvBJXT)
    TextView tvBJXT;
    @BindView(R.id.tvMHXT)
    TextView tvMHXT;
    @BindView(R.id.tvJKXT)
    TextView tvJKXT;

    Dialog dialog;
    String build_no;

//    @BindView(R.id.gvXiu)
//    GridView gvXiu;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_data_input_base_read);
        ButterKnife.bind(this);
        init();
    }

    @Override
    protected void onDestroy() {
        hideDialog(dialog);
        super.onDestroy();
    }

    public void init() {
        getIntentData();
        setTop();
        getData();
    }

    private void getData() {
        Map<String, String> params = new HashMap<String, String>();
        params.put(AppConfig.ACCESS_TOKEN,
                UserInfoUtil.getCurrentUserAccessToken());
        params.put("build_info_id", build_no);

        dialog = showDialog(dialog);
        VolleyRequest.StringRequestPost(this, WebApi.DATA_INPUT_BASE_READ, "",
                params, new StringInterface(this) {

                    @Override
                    public void stringSuccess(String result) {
                        try {
                            JSONObject obj = new JSONObject(result);
                            String status = obj.getString("status");
                            String info = obj.getString("info");
                            if (status.equals("200")) {
                                String data = obj.getString("data");
                                Log.e("dfdsf   ", result);
                                DataInputBaseBean temp = new Gson().fromJson(data,
                                        DataInputBaseBean.class);
                                setData(temp);
                            } else if (status.equals("404")
                                    && info.contains("尚未登录")) {
                                ToastUtil.getInstances().showMsg(
                                        "尚未登录或该用户在其他地方登录，请重新登录！");
                                // 清除环信登录
                                UserHelp.getInstance().clearUser(
                                        ApplicationBase.Context, true);
                                return;
                            } else {
                                ToastUtil.getInstances().showMsg(info);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        } finally {
                            hideDialog(dialog);
                        }
                    }

                    @Override
                    public void stringError(VolleyError error) {
                        hideDialog(dialog);
                        ToastUtil.getInstances().showMsg(
                                R.string.err_http_response);
                    }
                });
    }

    private void getIntentData() {
        build_no = getIntent().getStringExtra("build_no");
    }


    private void setTop() {
        tvTitle.setText("建筑基础属性");
        tvSearch.setCompoundDrawables(null, null, null, null);
        tvSearch.setVisibility(View.GONE);
    }

    @OnClick({R.id.tvBack, R.id.tvSearch})
    public void onClick(View view) {
        DateChooseDialog.Builder builder;
        switch (view.getId()) {
            case R.id.tvBack:
                finish();
                break;
            default:
                break;
        }
    }


    void setData(DataInputBaseBean dataInputBaseBean) {
        tvName.setText(dataInputBaseBean.getName());
        tvAddress.setText(dataInputBaseBean.getAddress());
        if (dataInputBaseBean.getType() == 0) {
            tvBuildType.setText("砖混结构");
        } else if (dataInputBaseBean.getType() == 1) {
            tvBuildType.setText("钢筋混凝土结构");
        } else if (dataInputBaseBean.getType() == 2) {
            tvBuildType.setText("框架结构");
        } else if (dataInputBaseBean.getType() == 3) {
            tvBuildType.setText("框剪结构");
        } else if (dataInputBaseBean.getType() == 4) {
            tvBuildType.setText("钢结构");
        } else if (dataInputBaseBean.getType() == 5) {
            tvBuildType.setText("核心筒结构");
        }

        tvCreate.setText(dataInputBaseBean.getYear());
        tvHeight.setText(dataInputBaseBean.getHeight());
        tvMianJi.setText(dataInputBaseBean.getArea());
        tvWYGLQK.setText(dataInputBaseBean.getProperty_management());
        tvJLR.setText(dataInputBaseBean.getManager());
        tvJLRTel.setText(dataInputBaseBean.getManager_phone());
        if (dataInputBaseBean.getFire_control_room_status() == 0) {
            tvJSQK.setText("未建设");
        } else if (dataInputBaseBean.getFire_control_room_status() == 1) {
            tvJSQK.setText("建设中");
        } else {
            tvJSQK.setText("已建设");
        }
        tvCZQK.setText(dataInputBaseBean.getWatchkeeper_condition());

        if (dataInputBaseBean.getAuto_alarm_system() == 0) {
            tvBJXT.setText("未安装");
        } else if (dataInputBaseBean.getAuto_alarm_system() == 1) {
            tvBJXT.setText("安装中");
        } else {
            tvBJXT.setText("已安装");
        }

        if (dataInputBaseBean.getAuto_water_system() == 0) {
            tvMHXT.setText("未安装");
        } else if (dataInputBaseBean.getAuto_water_system() == 1) {
            tvMHXT.setText("安装中");
        } else {
            tvMHXT.setText("已安装");
        }

        if (dataInputBaseBean.getEle_fire_mon_system() == 0) {
            tvJKXT.setText("未安装");
        } else if (dataInputBaseBean.getAuto_water_system() == 1) {
            tvJKXT.setText("安装中");
        } else {
            tvJKXT.setText("已安装");
        }

//        if (dataInputBaseBean.getImage() == null || dataInputBaseBean.getImage().size() <= 0) {
//            gvXiu.setVisibility(View.GONE);
//        } else {
//            gvXiu.setVisibility(View.VISIBLE);
//            //图片列表
//            List<String> picList = (List<String>) dataInputBaseBean.getImage();
//            if (picList != null) {
//                final String[] urls = new String[picList.size()];
//                for (int i = 0; i < picList.size(); i++) {
//                    urls[i] = picList.get(i);
//                }
//
//                gvXiu.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//
//                    @Override
//                    public void onItemClick(AdapterView<?> parent, View view,
//                                            int position, long id) {
//                        Intent intent = new Intent(view.getContext(), ImagePagerActivity.class);
//                        intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_URLS, urls);
//                        intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_INDEX, position);
//                        view.getContext().startActivity(intent);
//                    }
//                });
//                PicsAdapter picsAdapter = new PicsAdapter(gvXiu.getContext(), picList);
//                gvXiu.setAdapter(picsAdapter);
//            }
//        }
    }
}
